package com.jmlee.controller;

import com.jmlee.pojo.CommonPage;
import com.jmlee.pojo.CommonResult;
import com.jmlee.pojo.Menu;
import com.jmlee.pojo.Role;
import com.jmlee.service.MenuService;
import com.jmlee.service.RoleService;
import com.jmlee.util.SystemDateUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class RoleController {
    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    /**
     * 新增角色
     * @param role 角色
     * @return
     */
    @PostMapping("createNewRole")
    @ResponseBody
    public String createNewRole(@RequestBody Role role){
        String state = "0";
        role.setCreateTime(SystemDateUtil.getDaDate());
        state = roleService.addRole(role).toString();
        Long rId = role.getId();
        List<Menu> pMenuList = menuService.getAllParentMenuList();
        for (Menu menu:pMenuList) {
            roleService.addAuthority(rId,menu.getId());
        }
        List<Menu> cMenuList = menuService.getAllChildMenu();
        for (Menu menu:cMenuList) {
            roleService.addAuthority(rId,menu.getId());
        }
        return state;
    }

    /**
     *获取角色列表
     * @return
     */
    @GetMapping("getActiveRoleList")
    @ResponseBody
    public CommonResult getActiveRoleList(){
        return CommonResult.success(roleService.roleList(),"200");
    }
    /**
     *获取角色列表
     * @return
     */
    @PostMapping("getRoleList")
    @ResponseBody
    public CommonResult getRoleList(String curPage, String pageSize){
        List<Role> roleList = roleService.roleList(new RowBounds((Integer.parseInt(curPage)-1)*Integer.parseInt(pageSize),Integer.parseInt(pageSize)));
        CommonPage commonPage = new CommonPage();
//        commonPage.setCode(0);
//        commonPage.setMsg("success");
        commonPage.setCount(roleService.getCountOfRole());
        commonPage.setData(roleList);

        return CommonResult.success(commonPage,"ok");
    }

    /**
     * 删除角色
     * @param role
     * @return
     */
    @PostMapping("delRole")
    @ResponseBody
    public String delRole(@RequestBody Role role){
        role.setStatus(2);
         String status = roleService.deleteRole(role).toString();
        return status;
    }

    /**
     * 启用角色
     * @param role
     * @return
     */
    @PostMapping("enableRole")
    @ResponseBody
    public String enableRole(@RequestBody Role role){
        role.setStatus(1);
        String status = roleService.editRoleById(role).toString();
        return status;
    }

    /**
     * 禁用角色
     * @param role
     * @return
     */
    @PostMapping("disableRole")
    @ResponseBody
    public String disableRole(@RequestBody Role role){
        role.setStatus(0);
        String status = roleService.editRoleById(role).toString();
        return status;
    }


    @PostMapping("checkRoleName")
    @ResponseBody
    public String checkRoleName(@RequestBody Role role){

        return roleService.checkRoleName(role).toString();
    }

}
